package com.xkdx.caipiao.statics;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.xkdx.caipiao.pubclass.SelectCode;

public class IConstants {
	// 多请求地址
//	public static String addressV2 = "http://172.16.24.33/cpinter/index.php/Index/indexAll";
	public static String addressV2 = "http://cpinter.guangguang.net.cn/Index/indexAll";
	// public static String addressV2 =
	// "http://172.16.24.33/cp/Api/Index/indexAll";
	// 单请求地址
//	public static String address = "http://172.16.24.33/cpinter/index.php/Index/index";
	public static String address = "http://cpinter.guangguang.net.cn/Index/index";

	// 位置和经纬度
	public static String currentPosition;
	public static double lat = 0;
	public static double lon = 0;
	// 百度地图校验值
	public static double latCheck = 0.006004;
	public static double lonCheck = 0.006783;

	public static String ARRANGE_3 = "排列3";
	public static String ARRANGE_5 = "排列5";
	public static String SEVEN_COLOR = "七星彩";
	public static String LOTTO = "大乐透";
	public static ArrayList<SelectCode> list_selectcode = new ArrayList<SelectCode>(); // 大乐透
	public static ArrayList<SelectCode> list_three = new ArrayList<SelectCode>(); // 排列3
	public static ArrayList<SelectCode> list_five = new ArrayList<SelectCode>(); // 排列5
	public static ArrayList<SelectCode> list_sevencolor = new ArrayList<SelectCode>(); // 七星彩
	public static int FromRadioButton = -1;
	public static float localUserMoney = 0;
	public static int hongbaonum = 0;
	//key
	public static String key = "@#$%^&*(Adhgf)*duq81mdisoa^&$@$F";

	// 输入法隐藏
	public static void hideInputMethodAways(Context context, View view) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}

	// 设置listView的高度
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
		listView.setLayoutParams(params);
	}

	public static int getScreenWidth(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getWidth();
	}

	public static void Install(Context ctx) {
		Intent intentInstall = new Intent();
		String apkPath = "/data/data/" + ctx.getPackageName() + "/files";
		String apkName = "pay.apk";
		File file = new File(apkPath, apkName);
		try {
			// assets下对于超过2M 的文件，有所限制，建议改名为Jpg
			InputStream is = ctx.getAssets().open("UPPayPluginExPro2.1.4.apk");

			if (!file.exists()) {
				file.createNewFile();
				FileOutputStream os = ctx.openFileOutput(file.getName(),
						Context.MODE_WORLD_WRITEABLE);
				byte[] bytes = new byte[512];
				int i = -1;
				while ((i = is.read(bytes)) > 0) {
					os.write(bytes);
				}

				os.close();
				is.close();

			} else {

			}
			String permission = "666";

			try {
				String command = "chmod " + permission + " " + apkPath + "/"
						+ apkName;
				Runtime runtime = Runtime.getRuntime();
				runtime.exec(command);
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			Log.e("", e.toString());
		}
		Log.e("", "fl--" + file.getName() + "-dd---" + file.getAbsolutePath()
				+ "-pa-" + file.getPath());
		intentInstall.setAction(android.content.Intent.ACTION_VIEW);
		intentInstall.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		intentInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		ctx.startActivity(intentInstall);
	}

	public static String getCurrentTime() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd   HH:mm:ss");
		String date = sDateFormat.format(new java.util.Date());
		return date;
	}
	public static void download(Context context, String clientDownloadUrl) {
		try {

			PackageManager packageMgr = context.getPackageManager();

			List<PackageInfo> list = packageMgr.getInstalledPackages(0);
			for (int i = 0; i < list.size(); i++) {
				PackageInfo info = list.get(i);
				String temp = info.packageName;
				if (temp.equals("com.uc.browser")) {
					showUCBrowser(context, clientDownloadUrl);
					break;
				} else if (temp.equals("com.tencent.mtt")) {
					showQQBrowser(context, clientDownloadUrl);
					break;
				} else if (temp.equals("com.opera.mini.android")) {
					showOperaBrowser(context, clientDownloadUrl);
					break;
				} else if (temp.equals("com.android.browser")) {
					showGoogleBrower(context, clientDownloadUrl);
					break;
				}
			}
		} catch (Exception e) {
			Toast.makeText(context, "请先下载安装QQ、欧朋或UC浏览器...", Toast.LENGTH_LONG)
					.show();
		}
	}
	public static void showGoogleBrower(Context context, String url) {
		Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		it.setClassName("com.android.browser",
				"com.android.browser.BrowserActivity");
		context.startActivity(it);
	}

	public static void showUCBrowser(Context context, String url) {
		Intent intent = new Intent();
		intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(url));
		context.startActivity(intent);
	}

	public static void showQQBrowser(Context context, String url) {
		Intent intent = new Intent();
		intent.setClassName("com.tencent.mtt", "com.tencent.mtt.MainActivity");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(url));
		context.startActivity(intent);
	}

	public static void showOperaBrowser(Context context, String url) {
		Intent intent = new Intent();
		intent.setClassName("com.opera.mini.android",
				"com.opera.mini.android.Browser");
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setData(Uri.parse(url));
		context.startActivity(intent);
	}
}
